<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_TREE_MNG;


require "../framepages/moudle_base.php";

$species_id = $_GET['species_id'];
$species_record = \Data\Tree_species_DAO::getRecord($species_id);
$species_name = $species_record->getSPECIES_NAME();

$mode = $_POST['mode'];
$result = 'normal';
if ($mode == 'new_group_add') {

    $group_name = $_POST['new_group_name'];
    $group_same_name_record = \Data\Tree_group_DAO::getRecordByName($species_id,$group_name);
    if (!$group_name) {
        $result = 'unset_name';
    } elseif ($group_same_name_record) {
        $result = 'err_same_name';
    } else {
        $display_order = \Data\Tree_group_DAO::getMaxDis_orderByBySpecies_id($species_id)+1;

        $group_record = new \Data\Tree_group(array());
        $group_record->setGROUP_NAME($group_name);
        $group_record->setSPECIES_ID($species_id);
        $group_record->setDISPLAY_ORDER($display_order);
        \Data\Tree_group_DAO::insert($group_record);
        echo "
        <script>
            alert('添加成功');
            window.location.replace(location.href);
        </script>
    ";
    }
}

$mode_act = $_GET['mode_act'];
if ($mode_act == 'group_delete') {

    $group_id = $_GET['group_id'];
    $group_record = \Data\Tree_group_DAO::getRecord($group_id);
    $gg_array_string = $group_record->getGG_ARRAY();
    if ($gg_array_string) {
        echo "
        <script>
            alert('不能删除，该分组下还有规格');
            window.location.href = 'group_list.php?species_id='+$species_id;
        </script>
    ";
    } else {
        \Data\Tree_group_DAO::delete($group_record);
        echo "
        <script>
            alert('删除成功');
            window.location.href = 'group_list.php?species_id='+$species_id;
        </script>
    ";
    }
}


if ($mode_act == 'group_edit') {

    $group_id = $_GET['group_id'];
    $group_name = $_GET['group_name'];
    $display_order = $_GET['display_order'];
    $group_record = \Data\Tree_group_DAO::getRecord($group_id);

    $group_same_name_record = \Data\Tree_group_DAO::getIdArrByName($species_id,$group_name);
    if (!$group_name) {
        $result = 'unset_edit_name';
    } elseif (!$display_order) {
        $result = 'unset_edit_dis';
    } elseif ($group_same_name_record && !(in_array($group_id,$group_same_name_record))) {
        $result = 'err_edit_same_name';
    } else {
        $group_record->setGROUP_NAME($group_name);
        $group_record->setDISPLAY_ORDER($display_order);
        \Data\Tree_group_DAO::update($group_record);
        echo "
        <script>
            alert('修改成功');
            window.location.href = 'group_list.php?species_id='+".$species_id.";
        </script>
        ";
    }
}
?>
<div class="main-content">
    <div style="margin-bottom: 25px">
        <span>品种名称：</span>
        <span><?php echo $species_name ?></span>
    </div>
    <div>
        <table class="table" style="word-break:break-all">
            <tr>
                <th width="9%">序号</th>
                <th width="9%">分组id</th>
                <th width="9%">分组名称</th>
                <th width="9%">排列顺序</th>
                <th width="9%">规格列表</th>
                <th width="9%">修改</th>
                <th width="9%">删除</th>
            </tr>

            <?php
            $group_list = \Data\Tree_group_DAO::getListBySpecies_id($species_id);
            $i = 1;
            foreach ($group_list as $group_record) {
                $group_id = $group_record->getGROUP_ID();
                $group_name = $group_record->getGROUP_NAME();
                $display_order = $group_record->getDISPLAY_ORDER();
                echo "
                        <tr>
                            <td>$i</td>
                            <td>$group_id</td>
                            <td><input type='text' id='name_$group_id' name='group_name' value='$group_name'></td>
                            <td><input type='text' id='dis_$group_id' name='display_order' value='$display_order' size='3'></td>                     
                            <td><button type='button' class='btn btn-danger' onclick='group_gg_list($species_id,$group_id)' >规格列表</button></td>                            
                            <td><button type='button' class='btn btn-danger' onclick='group_edit($group_id)' >修改</button></td>                            
                            <td><button type='button' class='btn btn-danger' onclick='group_delete($group_id)' >删除</button></td>                            
                        </tr>
                    ";
                $i++;
            }
            ?>

        </table>
    </div>

    <?php
    require "../framepages/footer.php";
    ?>
    <div>
        <div>
            <span><h2>添加分组</h2></span>
        </div>
        <form action="" method="post" onSubmit="javascript:return window.confirm('确认提交吗？')">
            <input type="hidden" name="mode" id="mode" value='new_group_add'>
            <table>
                <tr>
                    <td>分组名称：</td>
                    <td><input type="text" name="new_group_name" placeholder='例如：胸径3-8cm'   ></td>
                </tr>
            </table>
            <div style="margin-top:30px;">
                <input type="submit" class="btn btn-danger" value="提交">
            </div>
        </form>
    </div>
</div>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script>
    function init() {
        var result = "<?php echo $result ?>";
        if (result == 'unset_name') {
            alert("添加失败，请输入分组名称");
        }

        if (result == 'err_same_name') {
            alert("添加失败，已存在同名称分组");
        }

        if (result == 'unset_edit_name') {
            alert("修改失败，请输入分组名称");
        }
        if (result == 'unset_edit_dis') {
            alert("修改失败，请输入分组排列顺序");
        }
        if (result == 'err_edit_same_name') {
            alert("修改失败，已存在同名称分组");
        }
        var mode = "<?php echo $mode ?>";
        var mode_act = "<?php echo $mode_act ?>";
        if (mode || mode_act) {
            window.location.href = 'group_list.php?species_id='+"<?php echo $species_id ?>";
        }
    }


    function group_gg_list(species_id,group_id) {
        var url = "group_gg_list.php?species_id="+species_id+"&group_id="+group_id;
        window.location.href = url;
    }

    function group_edit (group_id) {

        var group_name = document.getElementById('name_'+group_id).value;
        var display_order = document.getElementById('dis_'+group_id).value;

        var url = "group_list.php?species_id=<?php echo $species_id ?>&mode_act=group_edit&group_id="+group_id+"&display_order="+display_order+"&group_name="+group_name;
        window.location.href = url;
    }


    function group_delete(group_id) {
        if (window.confirm('确认删除吗?')) {
            var url = "group_list.php?species_id=<?php echo $species_id ?>&mode_act=group_delete&group_id="+group_id;
            window.location.href = url;
        }
    }

</script>
</body>
</html>
